﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Windvoice.Albums;

namespace Windvoice.Areas.Manage.Controllers
{
    public class AlbumController : ManagerControllerBase
    {
        //
        // GET: /Manage/Album/
        public ActionResult Index(int? page)
        {
            int PageIndex = page == null ? 1 : page.Value;
            int PageSize = 20, RecordCount = 0;
            ViewBag.AlbumList = Album.GetList(PageIndex, PageSize, out RecordCount);
            ViewBag.RecordCount = RecordCount;
            ViewBag.PageSize = PageSize;

            return View();
        }
        
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Album album)
        {
            album.CreateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                album.Create();
                return RedirectToAction("index");
            }
            return View();
        }

        public ActionResult Edit(int id)
        {
            var album = Album.Find(id);
            if (album != null)
            {
                return View(album);
            }
            return View("404");
        }

        [HttpPost]
        public ActionResult Edit(Album album)
        {
            var old = Album.Find(album.ID);
            album.CreateTime = old.CreateTime;
            album.FacePhoto = old.FacePhoto;

            if (ModelState.IsValid)
            {
                album.Update();
                return RedirectToAction("index");
            }
            return View();
        }

        [HttpPost]
        public ActionResult Delete(FormCollection formValues)
        {                
            if (!String.IsNullOrEmpty(formValues["id"]))
            {
                var AlbumID = Convert.ToInt32(formValues["id"]);
                Album.Find(AlbumID).Delete();
                return AjaxResult();
            }
            return AjaxResult(result: false, message: "参数错误");
        }

        public ActionResult Detail(int id)
        {
            var album = Album.Find(id);
            if (album != null)
            {
                ViewBag.PhotoList = album.GetPhotoes();
                return View(album);
            }
            return View("404");
        }
    }
}
